package com.dykj.fireFighting.web.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dykj.fireFighting.api.base.dev.entity.DevHisZc;
import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.DeviceDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.Device;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.ZcDataCleaning;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IDEA
 *
 * @Author : HXB
 * @Date : 2021-07-09 14:45
 * @Description : app端设备接口
 **/
@Slf4j
@Api(value = "app设备管理接口", tags = {"app设备管理接口"})
@RestController
@RequestMapping("/app/device")
public class AppDeviceController {

    @DubboReference
    private IDeviceService deviceService;
    @Autowired
    private RedisService redisService;

    /**
     * 设备管理-智能设备列表
     *
     * @return
     */
    @AutoLog(value = "设备管理-智能设备-分页列表查询")
    @ApiOperation(value = "设备管理-智能设备分页列表", notes = "设备管理-智能设备分页列表")
    @GetMapping("/list")
    @RequiresPermissions("device/list")
    public CommonResult<List<Device>> queryDeviceList(String deviceName) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        List<Device> deviceList = deviceService.getList(enterpriseId.toString(),deviceName);
        return CommonResult.success(deviceList);
    }
    /**
     * 设备管理-智能设备列表
     *
     * @return
     */
    @AutoLog(value = "设备管理-智能设备-分页列表查询")
    @ApiOperation(value = "设备管理-智能设备分页列表", notes = "设备管理-智能设备分页列表")
    @GetMapping("/listZt")
    public CommonResult<PageVo<Device>> treeGrid(DeviceDto deviceDto) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        deviceDto.setUnitId(enterpriseId+"");
        PageVo<Device> page = deviceService.pageList(deviceDto);
        for (int i = 0; i < page.getList().size(); i++) {
            page.getList().get(i).setStatus(null);
            String str = (String) redisService.get(page.getList().get(i).getRelationId()+"_realtime");
            if(StringUtils.isNotBlank(str)){
                JSONObject jsonMsg = JSON.parseObject(str);
                Object bjzt=jsonMsg.get("bjzt");
                if(null!=bjzt){
                    page.getList().get(i).setStatus(ZcDataCleaning.dataCleaning(bjzt.toString()));
                }

            }
        }
        return CommonResult.success(page);
    }
    /**
     * 设备管理-智能设备详情
     *
     * @return
     */
    @AutoLog(value = "设备管理-智能设备-智能设备详情")
    @ApiOperation(value = "设备管理-智能设备详情", notes = "设备管理-智能设备详情")
    @GetMapping("/detail")
    public CommonResult getDeviceDetail(String relationId){
        Object devicedetails = deviceService.getDevicedetails(relationId);
        return CommonResult.success(devicedetails);
    }
}
